#! /usr/bin/python
# -*- coding: cp1250 -*-

import glob, re, string, sys, os, time
import pepe.p_txt.bigtxt as bt
import pepe.p_dm.sdb as s

isoTimeNow = lambda : time.strftime ("%Y-%m-%dT%H:%M:%S", time.localtime())

class JoinPerKey:
    """ """
    def __init__(self, parameters):
        """ """
        self.ifmask = parameters['ifmask']
        self.cdelimiter = parameters['cdelimiter']
        self.skipheaders = parameters['skipheaders']
        self.output = open('jpk.log', 'w')
        self.data = {}
        self.ifiles = glob.glob(parameters['ifmask'])
        self.keys = []
        self.fcolumns = {}
       
    def examine(self):
        """ first find out all keys """
        # for each input file
        for ifile in self.ifiles:
            lino = 0

            # for each input line
            for line in bt.lines(ifile):
                line = re.sub('\n', '', line)
                lino += 1
    
                # line should have at least one cdelimiter
                if len(string.split(line, self.cdelimiter)) > 1:
    
                    # skipping headers
                    if lino == 1 and self.skipheaders == 'y':
                        pass
                    else:
                        cols = string.split(line, self.cdelimiter, 1)
                        #print cols

                        # for each file find remember columns count
                        cc = len(string.split(line, self.cdelimiter))
                        if ifile not in self.fcolumns.keys():
                            self.fcolumns[ifile] = cc
                        else:
                            if self.fcolumns[ifile] < cc:
                                self.fcolumns[ifile] = cc

                        k = cols[0]

                        idx = self.ifiles.index(ifile)
                        if k <> '':
                            ks = self.keys[:]
                            cd = self.cdelimiter
                            if k not in ks: 
                                self.keys.append(k)
                                self.data[k] = ['']*len(self.ifiles)
                                #self.data[k][idx] = ifile+cd+k+cd+cols[1]
                                self.data[k][idx] = k+cd+cols[1]
                            else:
                                #self.data[k][idx] = ifile+cd+k+cd+cols[1]
                                self.data[k][idx] = k+cd+cols[1]
        #print self.keys
        return self.keys

    def generate(self):
        """ """
        #print self.data.keys()
        for k in sorted(self.data.keys()):
            for index, cell in enumerate(self.data[k]):
                if cell == '': 
                    # columns count 
                    cc = self.fcolumns[self.ifiles[index]]
                    self.data[k][index] = self.cdelimiter*(cc)
            cd = self.cdelimiter
            #self.output.write(k+cd+string.join(self.data[k], cd)+'\n')
            self.output.write(string.join(self.data[k], cd)+"\n")
            #print k+cd+string.join(self.data[k], cd)
        self.output.close()
        print 'Done !'

def jpk(parameters):
    """
    Purpose:    Join rows per KEY column. 
                KEY column is expected to be in first column.
    Usage:      jpk <input file mask> <column delimiter> <skip_headers yn>
    Example:    jpk *.dat ^ y
    """
    print isoTimeNow()
    shdbPar = s.shlvDb('jpk.par')

    if parameters == {}:

        if shdbPar <> 'n/a':
            print 'Previous run parameters were (now default options):'
            print shdbPar

        parameters['ifmask'] = raw_input("Input files mask:")
        parameters['cdelimiter'] = raw_input("Column delimiter is:")
        parameters['skipheaders'] = raw_input("Should skip headers (y/n):")
        
        if parameters['ifmask'] == '': parameters['ifmask'] = shdbPar['ifmask']
        if parameters['cdelimiter'] == '': parameters['cdelimiter'] = shdbPar['cdelimiter']
        if parameters['skipheaders'] == '': 
            parameters['skipheaders'] = shdbPar['skipheaders']
        else:
            if parameters['skipheaders'] in ('y', 'Y'):
                parameters['skipheaders'] = 'y'
            else:
                parameters['skipheaders'] = 'n'

    s.shlvDb('jpk.par', parameters)

    x = JoinPerKey(parameters)
    x.examine()
    x.generate()
    print isoTimeNow()

if __name__ == '__main__':
    """ """
    #main('*.txt', "^")
    #main('*.txt', "^", "y")
    
    #ifm = r"""C:\AFT\root\rDisk\Clients\prague-faa\AMO\data\tb\*.dat"""
    #jpk(ifm, "\t", "y")

    #print sys.argv
    if len(sys.argv) == 2 and sys.argv[1] == '--help':
        print jpk.__doc__
        sys.exit(2)
    if len(sys.argv) == 4:
        jpk({'ifmask':sys.argv[1], 'cdelimiter':sys.argv[2], 'skipheaders':sys.argv[3]})
    elif len(sys.argv) == 3:
        jpk({'ifmask':sys.argv[1], 'cdelimiter':sys.argv[2], 'skipheaders':'n'})
    else:
        jpk({})
